package com.steven.ourcontest.information.view.teacher

import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.information.bean.UserDetail
import com.steven.ourcontest.information.viewmodel.IdentifyViewModel
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.utils.Identity
import com.steven.ourcontest.utils.changed
import com.steven.ourcontest.utils.toast
import com.steven.ourcontest.widget.SingleOptionsPicker
import kotlinx.android.synthetic.main.activity_confirm_information.*

class ConfirmTeacherInfoActivity : BaseActivity() {

    private var name: String = ""
    private var sex: String = ""
    private var school: String = ""
    private var college: String = ""
    private val identifyViewModel: IdentifyViewModel by viewModels()

    override fun getViewId(): Int? {
        return R.layout.activity_confirm_information
    }

    override fun getStatusBarColor(): Int {
        return R.color.white
    }

    override fun initView() {
        tv_title_identity?.text = "身份确认"
        rl_profession?.isVisible = false
        rl_year?.isVisible = false
        rl_select_educationalBackground?.isVisible = false
        val school = intent?.getStringExtra("school") ?: ""
        if (school == "") {
            finish()
            return
        }
        tv_school_name_selected?.text = school
        this.school = school
        et_name?.changed {
            this.name = et_name?.text.toString()
        }
        ll_select_sex?.setOnClickListener {
            val sexList = ArrayList<String>()
            sexList.add("男")
            sexList.add("女")
            SingleOptionsPicker.openOptionsPicker(
                this,
                sexList,
                1,
                tv_select_sex,
                R.layout.dialog_bottom_select_sex
            ) { options1, options2, options3, view ->
                tv_select_sex.text = sexList[options1]
                if (tv_select_sex?.text.toString() == "男" || tv_select_sex?.text.toString() == "女") {
                    sex = tv_select_sex?.text.toString()
                }
            }
        }
        et_college_name_selected?.changed {
            this.college = et_college_name_selected?.text.toString()
        }
        tv_confirm_all_info?.setOnClickListener {
            confirm()
        }
        identifyViewModel.addUserDetailResponse.observe(this, Observer {
            if (it?.msg == "success") {
                toast("提交成功，等待审核")
                AppRouter.gotoMainActivity(this, "me")
            } else {
                toast(it?.msg.toString())
            }
        })
    }

    private fun confirm() {
        if (!checkStrInfo(name)) {
            toast("姓名输入不合法！")
            return
        }
        if (!checkStrInfo(sex)) {
            toast("请选择性别！")
            return
        }
        if (!checkStrInfo(college)) {
            toast("学院输入不合法！")
            return
        }
        identifyViewModel.addUserDetail(
            name,
            school,
            college,
            userType = Identity.TEACHER.value
        )
    }

    private fun checkStrInfo(info: String?): Boolean {
        if (info != null) {
            if (info != "") {
                return true
            }
        }
        return false
    }
}